package com.toocms.jinjuhang.ui.mine.apply_cooperation

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.config.UploadType
import com.toocms.jinjuhang.data.ImageData
import com.toocms.tab.base.ItemViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class ApplyCooperationSupplementaryMaterialsImageItemModel(
    viewModel: ApplyCooperationModel,
    val item: (ObservableField<ImageData>) = ObservableField()
) :
    ItemViewModel<ApplyCooperationModel>(viewModel) {

    val itemClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (item.get()?.id.isNullOrEmpty()) {
            viewModel.selectImage(UploadType.SUPPLEMENTARY_MATERIALS)
        }
    })
    val deleteClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (!item.get()?.id.isNullOrEmpty()) {
            viewModel.supplementaryMaterialsImageItems -= this@ApplyCooperationSupplementaryMaterialsImageItemModel
        }
        if (viewModel.supplementaryMaterialsImageItems.isEmpty() || !TextUtils.isEmpty(viewModel.supplementaryMaterialsImageItems[0].item.get()?.id)) {
            viewModel.supplementaryMaterialsImageItems.add(
                0,
                ApplyCooperationSupplementaryMaterialsImageItemModel(viewModel, ObservableField(ImageData()))
            )
        }
    })
}